package cn.imvc.app.controllers.system;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import cn.imvc.app.entities.Role;
import cn.imvc.app.models.RoleModel;
import cn.imvc.app.models.RoleResourceModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;

public class ResourceRoleControllerImpl implements ResourceRoleController {
	public String roles;
	public String resources;
	public int id;
	public int[] roleid;
	public void setId(int id) {
		this.id = id;
	}

	@Autowired
	private RoleResourceModel roleResourceModel;
	
	@Autowired 
	private RoleModel roleModel;
	
	public String index() {
		resources=roleResourceModel.getResources();
		return "resource-role/index";
	}

	public String show() {
		roles=roleResourceModel.getRoleTree(id);
		return "resource-role/show";
	}

	public String create() {
		Set<Role> roles=new HashSet<Role>();
		if(roleid!=null)
		for (int rid : roleid) {
			roles.add(roleModel.get(rid));
		}
		roleResourceModel.saveResourceRole(id, roles);
		return Render.json(new DialogBean("200", "保存成功","role-resource","closeCurrent"));
	}

}
